﻿using System.Collections;

using UnityEngine;

namespace Assets.XyzCode.Fight.Game.Player
{
    partial class Player_MissDie_Behav
    {
        /** 快进携程 */
        private Coroutine _doFastForward_XC;

        /// <summary>
        /// 执行快进
        /// </summary>
        public void DoFastForward()
        {
            if (!CanSwitchStateTo(StateEnum.FastForward))
            {
                return;
            }

            if (null != _doFastForward_XC)
            {
                StopCoroutine(_doFastForward_XC);
            }

            _currState = StateEnum.FastForward;
            _doFastForward_XC = StartCoroutine(DoFastForward_XC());
        }

        /// <summary>
        /// ( 携程方式 ) 执行快进
        /// </summary>
        /// <returns>枚举迭代器</returns>
        private IEnumerator DoFastForward_XC()
        {
            Vector3 targetPos = transform.position;

            if (IsFaceToRight())
            {
                targetPos += Vector3.right * 4f;
            }
            else
            {
                targetPos += Vector3.left * 4f;
            }

            const float FAST_FORWARD_SPEED = 20f;

            // 播放粒子系统 1
            transform.Find("PSys_1_").GetComponent<ParticleSystem>().Play();
            // 播放快进动画
            PlayDBAnim("FastForward", 1, 1.5f);

            while (Vector3.Distance(transform.position, targetPos) > 0.1f)
            {
                transform.position = Vector3.MoveTowards(
                    transform.position,
                    targetPos,
                    FAST_FORWARD_SPEED * Time.deltaTime
                );
                yield return 1;
            }

            _doFastForward_XC = null;
            yield break;
        }
    }
}
